#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys

from PyQt5.Qt import QDialog, QApplication, Qt, QPalette,  QColor
from PyQt5.QtQuickWidgets import QQuickWidget

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import Qt,  QQuickView,  QUrl

from PyQt5.QtCore import pyqtSlot,  QObject,  pyqtSignal

from dae.qmlloader import QmlWindow
from dae.socketclient import SocketClient
from dae.manipulatedata import ManipulateData

class DAEClient(QObject):
    
    onMessage = pyqtSignal(int)
    
    def __init__(self, mainpath,  loginpath):
        super(DAEClient,  self).__init__()
        self.app = QApplication(sys.argv)
        self.app.setApplicationName("Health")
        
        self.login = QQuickView()
        self.login.setSource(QUrl(loginpath))
        
        
        self.loginObj = self.login.rootObject()
        self.login.setFlags(Qt.FramelessWindowHint)
        self.login.setColor(QColor(Qt.transparent))
        self.login.rootContext().setContextProperty("mainlogin",self.login)
        
        
        
        self.view = QQuickView()   
        self.view.setFlags(Qt.FramelessWindowHint)
        self.view.setColor(QColor(Qt.transparent))
        self.view.rootContext().setContextProperty("mainwindow",self.view)
        
        self.view.setSource(QUrl(mainpath))
        #self.view.show()
        self.viewObj = self.view.rootObject()
        self.manipulatedata = ManipulateData(self)
        
        self.initConnect()
        self.login.show()
        sys.exit(self.app.exec_())
        
    def initConnect(self):
        self.view.engine().quit.connect(self.app.quit)
        self.manipulatedata.onheartsignal.connect(self.viewObj.add_point)
        self.manipulatedata.onheartBPM.connect(self.viewObj.showBPM)
        self.manipulatedata.onheartIBI.connect(self.viewObj.showIBI)
        self.manipulatedata.onheartTemp.connect(self.viewObj.showTemp)
        self.manipulatedata.ongethealthrespond.connect(self.viewObj.initHListView)
        self.manipulatedata.onloginsuccess.connect(self.show)
        self.viewObj.close.connect(self.close)
        self.viewObj.postClicked.connect(self.manipulatedata.postHealthMsg)
        self.loginObj.login.connect(self.manipulatedata.postLoginMsg)
    
    def show(self):
        self.view.show()
        self.login.close()
    
    def close(self,  message):
        print (message)
        self.login.close()
        self.view.close()
    
    @pyqtSlot(str)
    def receiveMessage(self, data):
        Data = int(data[2:])
        #print (Data)
        self.onMessage.emit(Data)
        
        
 
    

        



#app_dir = os.path.realpath(app_dir)
#app = QApplication(sys.argv)
#w = QmlWindow("file:///media/G/qt/test2/qml/test2/main.qml")
		#app.setApplicationName("Health")

#sys.exit(app.exec_)
